package spring.core.examples.validator.xml;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import spring.core.examples.xml.Person;

/**
 * Implements a validator for a person using the Validator interface. In this
 * case all the validation is done using Java code.
 */
public class PersonValidator implements Validator {

	/**
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	public boolean supports(Class<?> clazz) {
		return Person.class.equals(clazz);
	}

	/**
	 * @see org.springframework.validation.Validator#validate(java.lang.Object,
	 *      org.springframework.validation.Errors)
	 */
	public void validate(Object target, Errors errors) {
		Person myToBeValidated = (Person) target;
		if (myToBeValidated.getAge() <= 0) {
			errors.rejectValue("age", "negativevalue");
		}

	}

}
